﻿using DDLParser.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDLParser
{
    public class TermComparator :IComparer<DDLTerm>
    {
        public int Compare(DDLTerm x, DDLTerm y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're 
                    // equal.  
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y 
                    // is greater.  
                    return -1;
                }
            }
            else if (y == null)
            {
                // If x is not null... 
                // 
                return 1;
            }
            else
            {
                // ...and y is not null, compare the  
                // lengths of the two strings. 
                // 
                int retval = x.Term.Length.CompareTo(y.Term.Length);

                if (retval != 0)
                {
                    // If the strings are not of equal length, 
                    // the longer string is greater. 
                    // 
                    return retval;
                }
                else
                {
                    // If the strings are of equal length, 
                    // sort them with ordinary string comparison. 
                    // 
                    return x.Term.CompareTo(y.Term);
                }
            }
        }
    }
}
